using System.Collections.ObjectModel;
using Microsoft.Xna.Framework;
using System;

namespace Core.Graphics
{
    public class TileSetTile
    {
        public virtual int Height { get { return _SourceRectangle.Height; } }
        public virtual int Width { get { return _SourceRectangle.Width; } }

        public virtual int ID { get { return _ID; } }

        public virtual Tile Tile { get { return new Tile(_SpriteID); } }
        public virtual TileSet Parent { get { return _Parent; } }
        public virtual Rectangle SourceRectangle { get { return _SourceRectangle; } }
        public virtual TileID SpriteID { get { return _SpriteID; } set { _SpriteID = value; } }

        private int _ID = -1;
        private TileSet _Parent = null;
        private Rectangle _SourceRectangle = Rectangle.Empty;
        private TileID _SpriteID = TileID.EmptyID;

        public TileSetTile(int ID, TileSet Parent)
        {
            _ID = ID;
            _Parent = Parent;
            _SpriteID = new TileID(ID, _Parent);

            //Get the rectangle to draw the tile from
            GetRectangle();
        }

        public TileSetTile() {
            //xml deserialize
        }

        protected virtual void GetRectangle()
        {
            if (_Parent == null)
                return;

            int tileX = _ID % _Parent.TilesPerRow;
            int tileY = _ID / _Parent.TilesPerRow;

            Rectangle rectSource = new Rectangle(
                (tileX * _Parent.TileWidth) + _Parent.HorizontalSpacing * tileX,
                (tileY * _Parent.TileHeight) + _Parent.VerticalSpacing * tileY,
                _Parent.TileWidth, _Parent.TileHeight);

            _SourceRectangle = rectSource;
        }

        public static TileSetTile BlankTile
        {
            get { return new TileSetTile(-1, null); }
        }
    }

    [Serializable()]
    public class TileSetTileCollection : Collection<TileSetTile>
    {

    }
}
